jQuery(function($){
	var productPackage={
			grid : null,
			init:function(){
				/*if(roles.length>0){
					var items = [];		
					var exportExcel = [];
					for(var i=0; i<roles.length; i++) {
						if (roles[i] == 'GOOD_PERSON' || roles[i] == 'SYSTEM_ADMIN' || roles[i] == 'GOODS_SUPERVISOR') {
							exportExcel = [{text: "导出EXCEL",
								id:"export", 
								click: function(){
									productPackage.exportExcel();
								}, icon: "up"},{ line: true }];
			    		}
					}
					items = items.concat(exportExcel);
					toolbar = {items : items};
				}*/
				var items = [];		
				var exportExcel = [];
				exportExcel = [{text: "导出EXCEL",
					id:"export", 
					click: function(){
						productPackage.exportExcel();
					}, icon: "up"},{ line: true }];
				items = items.concat(exportExcel);
				toolbar = {items : items};
				$("#btnSearch").on("click",function(){
					productPackage.grid.set({
						parms: [
								{name: "channelOrderId", value:$("#channelOrderId").val()},
								{name: "channelOrderType", value:$("#channelOrderType").val()},
								{name: "prodPackageNo", value:$("#prodPackageNo").val()},
								{name: "deliveryState", value:$("#deliveryState").val()},
								/*{name: "payState", value:$("#payState").val()},
								{name: "payType", value:$("#payType").val()},*/
								{name: "orderStartTime",value:getFullTime($("#orderStartTime").val(),true)},
								{name: "orderEndTime", value:getFullTime($("#orderEndTime").val(),false)}
			                    ],
						url: "productPackagePageList.do"
					});
					//productPackage.grid.loadData();					
				});
				this.pageList();
				this.channelOrderType=[
				    {value:0,text:"京东"},
				    {value:1,text:"淘宝"},
				    {value:2,text:"天猫"}
				];
				this.orderState=[
					{value:0,text:"已提交"},
					{value:1,text:"审核通过"},
					{value:2,text:"审核不通过"},
					{value:3,text:"待支付"},
					{value:4,text:"待发货"},
					{value:5,text:"已发货"},
					{value:6,text:"已完成"},
					{value:7,text:"已取消"},
					{value:8,text:"已锁定"}
				];
				this.deliveryState=[
				    {value:0,text:"未发货"},
				    {value:1,text:"已发货"}              
				];
				this.payState=[
				    {value:0,text:"未支付"},
				    {value:1,text:"已支付"}              
				];
				this.payType=[
				    {value:1,text:"货到付款"},
				    {value:2,text:"邮局汇款"},
				    {value:3,text:"自提"},
				    {value:4,text:"在线支付"},   
				    {value:5,text:"公司转账"},
				    {value:6,text:"银行卡转账"} 
				];
				this.orderType=[
					{value:0,text:"白拿"},
					{value:1,text:"返还"},
					{value:2,text:"天猫"}
				];
				this.state=[
					{value:0,text:"未出库"},
					{value:1,text:"已生成出库单"},
					{value:2,text:"已出库"}
				];
				//this.setSelectBox(this.payState,$("#payState"));
				//this.setSelectBox(this.payType,$("#payType"));
				this.setSelectBox(this.channelOrderType,$("#channelOrderType"));
				this.setSelectBox(this.deliveryState,$("#deliveryState"));
			},
			pageList:function(){
				productPackage.grid = $("#maingrid").ligerGrid({
		            columns: [
		            { display: '包裹编号', name: 'prodPackageNo'},
		            { display: '订单编号', name: 'channelOrderId'},
		            { display: '订单渠道', name: 'channelOrderType', 
		              render: function (rowData, rowindex, value){
		            	    return productPackage.getTextByValue(productPackage.channelOrderType,value);
				    }},
				    { display: '仓库', name: 'whName'},
				    /*{ display: '订单状态', name: 'orderState', 
			          render: function (rowData, rowindex, value){
			            	return productPackage.getTextByValue(productPackage.orderState,value);
			        }},*/
		            { display: '出库状态', name: 'state', 
			              render: function (rowData, rowindex, value){
			            	    return productPackage.getTextByValue(productPackage.state,value);
			        }},
		            /*{ display: '发货状态', name: 'deliveryState', 
		              render: function (rowData, rowindex, value){
		            	    return productPackage.getTextByValue(productPackage.deliveryState,value);
		            }},*/
		            { display: '订单类型', name: 'channelOrderType',
		            	render: function (rowData, rowindex, value){
		            		return productPackage.getTextByValue(productPackage.orderType,value);
				    }},		            
		            /*{ display: '支付状态', name: 'payState',
		            	render: function (rowData, rowindex, value){
		            		return productPackage.getTextByValue(productPackage.payState,value);
			        }},
		            { display: '支付方式', name: 'payType',
		            	render: function (rowData, rowindex, value){
		            		if(value){
		            			//value=value.substring(0,1);
		            			return productPackage.getTextByValue(productPackage.payType,value);
		            		}
		            		return "";
				    }},*/
		            { display: '发货时间', name: 'deliveryTime'},
		            { display: '操作', isSort: false, render: function (rowdata, rowindex, value){
		            	var stockOut = "<a href='javascript:stockOut(" + rowindex + ",1)'>出库</a> ";
		            	var h = "<a href='javascript:showPackageInfo(" + rowindex + ",1)'>包裹信息</a> ";
		            	for ( var i in roles) {
		            		//商品专员,商品主管,管理员
		               		if(roles[i] == 'GOOD_PERSON' || roles[i] == 'SYSTEM_ADMIN' || roles[i] == 'GOODS_SUPERVISOR'){
		               			var css = "";
			             		if(rowdata.checkState == 1) css = "style='color: gray;'";
			             		
			             		h += "<a href='javascript:setCommodityInfo(" + rowindex + ",2)' "+css+">设置供应商</a> ";
			             	    if(rowdata.checkState != 1){
			                    	h += "<a href='javascript:updateCommodity(" + rowindex + ")' >修改</a> ";
			                    }
			                     
			                    h += "<a href='javascript:delCommodity(" + rowindex + ")' "+css+">删除</a> ";
		               		}
		        		}
		            	
		            	if(rowdata.state){
		            		return h;
		            	}
		            	
		             	if(rowdata.deliveryState==0){
		             		return stockOut+h;
		             	}
		            
		                return h;
		            	}
		            }
		            ], 
		            pageSize:30,
		            height: '100%',
		            dataType: 'server', 
		            dataAction: 'server',
		            pagesizeParmName: 'pageSize',
		            parms: [
							{name: "channelOrderId", value:$("#channelOrderId").val()},
							{name: "channelOrderType", value:$("#channelOrderType").val()},
							{name: "prodPackageNo", value:$("#prodPackageNo").val()},
							{name: "deliveryState", value:$("#deliveryState").val()},
							/*{name: "payState", value:$("#payState").val()},
							{name: "payType", value:$("#payType").val()},*/
							{name: "orderStartTime", value:$("#orderStartTime").val()},
							{name: "orderEndTime", value:$("#orderEndTime").val()}
		                    ],
					url: "productPackagePageList.do",
					toolbar: toolbar
		        });
				grid=productPackage.grid;
			},
			setSelectBox:function(objs,dom){
				if(objs  && objs.length){
					dom.html('<option value="">请选择</option>');
					for(var x in objs){
						dom.append('<option value="'+objs[x].value+'">'+objs[x].text+'</option>');
					}
				}
			},
			getTextByValue:function(objs,value){
				if(objs && objs.length){
					for(var x in objs){
						if(objs[x].value==value){
							return objs[x].text;
						}
					}
				}
			},
			exportExcel:function(){
				var exportUrl = basePath + "/order/channelOrder/exportProductPackage.do";
				var params=[{name: "channelOrderId", value:$("#channelOrderId").val()},
							{name: "channelOrderType", value:$("#channelOrderType").val()},
							{name: "prodPackageNo", value:$("#prodPackageNo").val()},
							{name: "deliveryState", value:$("#deliveryState").val()},
							{name: "orderStartTime", value:$("#orderStartTime").val()},
							{name: "orderEndTime", value:$("#orderEndTime").val()}
		                    ];
	            var form = $("<form>");   //定义一个form表单
	            form.attr('style', 'display:none');   //在form表单中添加查询参数
	            form.attr('target', '');
	            form.attr('method', 'post');
	            form.attr('action', exportUrl);
	            for(var i=0;i<params.length;i++){
	            	var input = $('<input>');
		            input.attr('type', 'hidden');
		            input.attr('name', params[i].name);
		            input.attr('value', params[i].value);
		            form.append(input);   //将查询参数控件提交到表单上
	            }
	            $('body').append(form);  //将表单放置在web中 
	            form.submit();
	            form.remove();
			}
	};
	productPackage.init();
}); 